import store from "../store/index";

export const beforeEach = (to, from, next) => {
  const needlessLogin = to.path === "/login" || to.path === "/logon";
  const needLoign = to.path.indexOf("/user") !== -1;
  if (needLoign && !store.getters.isLogin) {
    store.commit("setCommonState", {
      key: "path",
      value: "/login"
    });
    next("/login");
  } else if (needlessLogin && store.getters.isLogin) {
    store.commit("setCommonState", {
      key: "path",
      value: "/"
    });
  } else {
    store.commit("setCommonState", {
      key: "path",
      value: to.path
    });
    next();
  }
};
